import PubSub from 'pubsub-js'
import moment from 'moment'
import request from '../../utils/request'
Page({
  boxClick(e) {
    // console.log(e, 'a')
  },
  //通过id获取歌曲详情
  async getMusicDetail(id) {
    if (this.data?.recommendSongId) {
      const musicDetail = await request({
        url: '/song/detail',
        data: {
          ids: id
        }
      })
      if (musicDetail.code === 200) {
        const musicName = musicDetail.songs[0].name
        wx.setNavigationBarTitle({
          title: musicName
        })
        //修改标题
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: '#d43c33',
        })
        this.setData({
          musicDetail
        })
      }
    }
  },

  //获取音乐播放地址
  async getMusicUrl(id) {
    const musicUrl = await request({
      url: "/song/url",
      data: {
        id
      }
    })
    if (musicUrl.code === 200) {
      this.backgroundAudioManager = wx.getBackgroundAudioManager()
      //没有会员拿不到url
      if (musicUrl.data[0].url) {
        this.setData({
          musicUrl: musicUrl.data[0].url
        })
        //不需要会员的，直接播放
        //改变播放状态
        this.setData({
          isPlay: true
        })
        this.backgroundAudioManager.src = musicUrl.data[0].url
        this.backgroundAudioManager.title = this.data.musicDetail.songs[0].al.name
        //当前音频长度
        // const duration
        if(this.backgroundAudioManager.duration%60>9){
         const duration = Math.floor(Number(this.backgroundAudioManager.duration)/60)+':'+parseInt(this.backgroundAudioManager.duration%60)
          this.setData({
            duration
          })
        }else{
         const duration = Math.floor(Number(this.backgroundAudioManager.duration)/60)+':0'+parseInt(this.backgroundAudioManager.duration%60)
          this.setData({
            duration
          })
        }
        
        
        //监听进度条
        this.backgroundAudioManager.onTimeUpdate(()=>{
          // console.log(this.backgroundAudioManager.currentTime)
          const progressWidth = this.backgroundAudioManager.currentTime/this.backgroundAudioManager.duration*100 +'%'
          this.setData({
            progressWidth
          })
        })
      } else {
        wx.showLoading({
          title: '需要会员',
        })
      }
    }
  },
  //点击播放暂停和继续播放
  handlerPlayOrPause() {
    this.setData({
      isPlay: this.data.isPlay ? false : true
    })
    if (this.data.isPlay) {
      //延迟一秒等杆子放好再放
      this.time = setTimeout(() => {
        this.backgroundAudioManager.play()
      }, 1000)
      this.setData({
        transform: 'transform: rotate(8deg)'
      })
    } else {
      // 暂停清空计时器，防止暂停了还会播放
      clearTimeout(this.time)
      this.backgroundAudioManager.pause()
      this.setData({
        transform: 'transform: rotate(-8deg)'
      })
    }
  },
  //获取本地id数组
  getStorageIdList() {
    const res = wx.getStorageSync('idList')
    this.setData({
      idList: res
    })
  },
  // 点击上一曲下一曲
  handlerSwitch(e) {
    const currentId = this.data.recommendSongId
    // 当前id
    const currentIndex = this.data.idList.indexOf(Number(currentId))
    if (e.target.id === 'prev') {
      const prevId = this.data.idList[currentIndex - 1]
      if(currentIndex-1<0){
        this.setData({
          recommendSongId:this.data.idList[this.data.idList.length-1]
        })
      }else{
        this.setData({
          recommendSongId:prevId
        })
      }
      this.getMusicDetail(this.data.recommendSongId)
      this.getMusicUrl(this.data.recommendSongId)
    }else{  //下一曲
      const nextId = this.data.idList[currentIndex + 1]
      if(currentIndex+1>this.data.idList.length-1){
        this.setData({
          recommendSongId:this.data.idList[0]
        })
      }else{
        this.setData({
          recommendSongId:nextId
        })
      }
      this.getMusicDetail(this.data.recommendSongId)
      this.getMusicUrl(this.data.recommendSongId)
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    musicDetail: {}, //歌曲详情列表
    idList: [],  //歌曲id列表
    recommendSongId: '', //推荐歌曲id
    musicUrl: '', //歌曲播放地址
    isPlay: false, //控制音乐播放和暂停
    transform: 'transform: rotate(8deg)',
    transition: 'transition: all 1s linear',
    progressWidth:'0%' ,//进度条
    duration:''  //歌曲总的时长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.getStorageIdList()
    if (options) {
      this.setData({
        recommendSongId: options.id
      })
    }
    await this.getMusicDetail(this.data.recommendSongId)
    //因为下面用到了上面的数据，所以要保证data中有数据  
    this.getMusicUrl(this.data.recommendSongId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log(777777)
    // PubSub.clearAllSubscriptions()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})